let app = getApp();
Page({
  data: {
    logs: [],
    indexImg: [],
    openid:'',
    openidid:'',
    shopId:'',
    indicatorDots: true,
    vertical: true,
    autoplay: true,
    interval: 4000,
    duration: 1500
  },
  login(res) {
    let that = this;
    let loginType=null;
    let {
      iv,
      encryptedData
    } = res.detail;
  

    wx.getStorage({
      key: 'openid',
      success(res) {        
        if(res.data){
          loginType = 'loginopenid';
        }else{
          loginType = 'loginPhone'
        }
      },fail(res){
        loginType = 'loginPhone'
      }
    });
    if (iv && encryptedData) {
      wx.showLoading();
      wx.login({
        success(e) {
          let code = e.code;
          wx.request({
            url: 'https://www.yinuoled.com/webapi/login.ashx',
            method: "get",
            data: {
              iv,
              code,
              encryptedData,
              openid: that.openid,
              openidid: that.openidid,
              action: loginType
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res){
              let roleId ='';
              if (res.statusCode===200){
                app.toast(res.data.msg);
                setTimeout(()=>{
                  wx.hideLoading();
                  wx.setStorage({
                    key: 'openid',
                    data: res.data.openid,
                  });
                  wx.setStorage({
                    key: 'yewuyuanPhone',
                    data: res.data.yewuyuandianhua,
                  });
                  wx.setStorage({
                    key: 'post',
                    data: res.data.zhiwei,
                  });
                  //将部门名称存储
                  wx.setStorage({
                    key: 'bumenmingcheng',
                    data: res.data.bumenmingcheng,
                  });
                  //将角色存储
                  wx.setStorage({
                    key: 'navFlag',
                    data: res.data.juese,
                  });
                  //将姓名存储
                  wx.setStorage({
                    key: 'supervisorName',
                    data: res.data.xingming,
                  });

                  //判断是否存在商品id,是否未分享后需要登陆
                  if (that.data.shopId){
                    wx.reLaunch({
                      url: '/pages/detail/detail?shopId=' + that.data.shopId,
                    });
                    return false;
                  }
                  wx.redirectTo({
                    url: '/pages/home/home'
                  });
                },500);
              }else{
                wx.hideLoading();
                app.toast(res.errMsg);
              }            
            },
            fail(){
              wx.hideLoading();
            }
          });        
        },
        fail() {
          app.toast("未知错误,重新获取中");
        }
      })
    }
   
  },
  onShow() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res){
        if(res.data){
          that.openid = res.data;
          wx.redirectTo({
            url: '/pages/home/home',
          });      
        }
      },
    });
    wx.getStorage({
      key: 'salesmanOpenid',
      success(res) {
        that.openidid = res.data;
      }
    });
  },
  onLoad(opt){
    console.log(opt);
    let that = this;
    if (opt.shopFlag === 'cart') {
      that.setData({
        shopId: opt.shopId
      });
    }
    wx.request({
      url: 'https://www.yinuoled.com/webapi/GetLunBo.ashx',
      method: "get",
      data: {
        action: 'gethuanyin'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        that.setData({
          indexImg: res.data
        });
      }
    });
  },
})